/*
 * @(#)OfflineStateInfoDaoImpl.java   1.0  2021年11月17日
 * 
 * Copyright (c)	2014-2020. All Rights Reserved.	GuangZhou hhmk Technology Company LTD.
 */
package com.swift.dts.dao.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.swift.dts.dao.OfflineStateInfoDao;
import com.swift.dts.dao.model.OfflineState;
import com.swift.dts.dao.model.OfflineStateInfo;
import com.swift.dts.dao.model.SynObjectKey;
import com.swift.dts.db.LocalDbDataSource;
import com.swift.util.date.DateUtil;
import com.swift.util.type.JsonUtil;
import com.swift.util.type.TypeUtil;

/**
 * 添加说明
 * 
 * @author zhengjiajin
 * @version 1.0 2021年11月17日
 */
@Component
public class OfflineStateInfoDaoImpl implements OfflineStateInfoDao {

    @Autowired
    private LocalDbDataSource localDbDataSource;

    /**
     * @see com.swift.dts.dao.OfflineStateInfoDao#save(com.swift.dts.dao.model.OfflineStateInfo)
     */
    @Override
    public void save(OfflineStateInfo offlineStateInfo) {
        String sql = "INSERT INTO offline_state_info(destination,data_source,db_name,table_name,state,error_rem,success_num,count_num,percent,create_time) VALUES("
            + "'" + offlineStateInfo.getDestination() + "'" + ",'" + offlineStateInfo.getDataSource() + "'" + ",'"
            + offlineStateInfo.getDbName() + "'" + ",'" + offlineStateInfo.getTableName() + "'" + ","
            + offlineStateInfo.getState() + ",'" + TypeUtil.toString(offlineStateInfo.getErrorRem(), "") + "'" + ","
            + TypeUtil.toInt(offlineStateInfo.getSuccessNum(), 0) + ","
            + TypeUtil.toInt(offlineStateInfo.getCountNum(), 0) + ",0" + ",'" + offlineStateInfo.getCreateTime() + "'"
            + ")";
        localDbDataSource.execute(sql);
    }

    /**
     * @see com.swift.dts.dao.OfflineStateInfoDao#list()
     */
    @Override
    public List<OfflineStateInfo> list() {
        String sql = "SELECT * FROM offline_state_info ";
        List<Map<String, Object>> list = localDbDataSource.queryForList(sql);
        if (TypeUtil.isNull(list)) return null;
        return JsonUtil.toListObject(JsonUtil.toJson(list), OfflineStateInfo.class);
    }

    /**
     * @see com.swift.dts.dao.OfflineStateInfoDao#list(com.swift.dts.dao.model.SynObjectKey)
     */
    @Override
    public List<OfflineStateInfo> list(String dataSource, String destination, Integer state) {
        String sql = "SELECT * FROM offline_state_info " + where(dataSource, destination, state);
        List<Map<String, Object>> list = localDbDataSource.queryForList(sql);
        if (TypeUtil.isNull(list)) return null;
        return JsonUtil.toListObject(JsonUtil.toJson(list), OfflineStateInfo.class);
    }

    private String where(String dataSource, String destination, Integer state) {
        StringBuffer sb = new StringBuffer();
        if (TypeUtil.isNotNull(destination)) {
            if (sb.length() > 0) sb.append(" AND ");
            sb.append(" destination='" + destination + "' ");
        }
        if (TypeUtil.isNotNull(dataSource)) {
            if (sb.length() > 0) sb.append(" AND ");
            sb.append(" data_source='" + dataSource + "' ");
        }
        if (TypeUtil.isNotNull(state)) {
            if (sb.length() > 0) sb.append(" AND ");
            sb.append(" state=" + state + " ");
        }
        if (sb.length() <= 0) return " ";
        return " WHERE " + sb.toString();
    }

    /**
     * @see com.swift.dts.dao.OfflineStateInfoDao#delete(com.swift.dts.dao.model.SynObjectKey)
     */
    @Override
    public void delete(SynObjectKey synObjectKey) {
        String sql = "DELETE FROM offline_state_info " + where(synObjectKey);
        localDbDataSource.execute(sql);
    }

    /**
     * @see com.swift.dts.dao.OfflineStateInfoDao#updateState(com.swift.dts.dao.model.SynObjectKey, int)
     */
    @Override
    public void updateState(SynObjectKey synObjectKey, OfflineState state, String errorRem) {
        StringBuffer sb = new StringBuffer("UPDATE offline_state_info SET ");
        sb.append("state=" + state.getState());
        sb.append(", error_rem='" + TypeUtil.toString(errorRem, "") + "' ");
        if (state.equals(OfflineState.NO_START)) {
            sb.append(", update_time=null ");
        } else {
            sb.append(", update_time='" + DateUtil.formatDate(new Date()) + "' ");
        }
        sb.append(where(synObjectKey));
        localDbDataSource.execute(sb.toString());
    }

    /**
     * @see com.swift.dts.dao.OfflineStateInfoDao#updatePercent(com.swift.dts.dao.model.SynObjectKey, double)
     */
    @Override
    public void updatePercent(SynObjectKey synObjectKey, Integer successNum, Integer countNum, Double percent) {
        StringBuffer sb = new StringBuffer("UPDATE offline_state_info SET ");
        sb.append(" update_time='" + DateUtil.formatDate(new Date()) + "' ");
        if (successNum != null) sb.append(", success_num=" + successNum);
        if (countNum != null) sb.append(", count_num=" + countNum);
        if (percent != null) sb.append(", percent=" + percent);
        sb.append(where(synObjectKey));
        localDbDataSource.execute(sb.toString());
    }

    private String where(SynObjectKey synObjectKey) {
        if (synObjectKey == null) return " ";
        StringBuffer sb = new StringBuffer();
        if (TypeUtil.isNotNull(synObjectKey.getDestination())) {
            if (sb.length() > 0) sb.append(" AND ");
            sb.append(" destination='" + synObjectKey.getDestination() + "' ");
        }
        if (TypeUtil.isNotNull(synObjectKey.getDataSource())) {
            if (sb.length() > 0) sb.append(" AND ");
            sb.append(" data_source='" + synObjectKey.getDataSource() + "' ");
        }
        if (TypeUtil.isNotNull(synObjectKey.getDbName())) {
            if (sb.length() > 0) sb.append(" AND ");
            sb.append(" db_name='" + synObjectKey.getDbName() + "' ");
        }
        if (TypeUtil.isNotNull(synObjectKey.getTableName())) {
            if (sb.length() > 0) sb.append(" AND ");
            sb.append(" table_name='" + synObjectKey.getTableName() + "' ");
        }
        if (sb.length() <= 0) return " ";
        return " WHERE " + sb.toString();
    }

    /**
     * @see com.swift.dts.dao.OfflineStateInfoDao#get(com.swift.dts.dao.model.SynObjectKey)
     */
    @Override
    public OfflineStateInfo get(SynObjectKey synObjectKey) {
        String sql = "SELECT * FROM offline_state_info " + where(synObjectKey);
        Map<String, Object> map = localDbDataSource.queryForMap(sql);
        if (TypeUtil.isNull(map)) return null;
        return JsonUtil.toObj(JsonUtil.toJson(map), OfflineStateInfo.class);
    }

}
